package com.gouaixin.bee.beeframework2.dao;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.bean.MSG;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/8.
 */
//消息对象数据库操作器
public class MsgDao {

    public static void add(Context context, MSG bean){
        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            db.saveBindingId(bean);
        } catch (DbException e) {
            Log.e("db", e.getMessage());
        }
    }





    public static List<MSG> query(Context context){
        List<MSG> list =new ArrayList<MSG>();

        try {

            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);


            list = db.findAll(Selector.from(MSG.class));//.where(WhereBuilder.b("name", "=", "child' name")));
            Log.e("db",new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }

    public static List<MSG> queryWithId(Context context,String jid,String cid){
        List<MSG> list =new ArrayList<MSG>();

        try {

            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            list = db.findAll(Selector.from(MSG.class).where(WhereBuilder.b("jid", "=", jid).and("cid","=",cid)));//.where("jid","=",jid).and("cid","=",cid));//.where(WhereBuilder.b("name", "=", "child' name")));
            Log.e("db",new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }

}
